
/*
===============================================================================
                    EntitySpaces Studio by EntitySpaces, LLC
             Persistence Layer and Business Objects for Microsoft .NET
             EntitySpaces(TM) is a legal trademark of EntitySpaces, LLC
                          http://www.entityspaces.net
===============================================================================
EntitySpaces Version : 2012.1.0930.0
EntitySpaces Driver  : SQL
Date Generated       : 2016/3/21 1:39:13
===============================================================================
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using EntitySpaces.Core;
using EntitySpaces.Interfaces;
using EntitySpaces.DynamicQuery;



namespace yncTech.BusinessObjects
{
	/// <summary>
	/// Encapsulates the 'T_S_System' table
	/// </summary>

    [DebuggerDisplay("Data = {Debug}")]
	[Serializable]
	[DataContract]
	[KnownType(typeof(TSSystem))]	
	[XmlType("TSSystem")]
	public partial class TSSystem : esTSSystem
	{	
		[DebuggerBrowsable(DebuggerBrowsableState.RootHidden | DebuggerBrowsableState.Never)]
		protected override esEntityDebuggerView[] Debug
		{
			get { return base.Debug; }
		}

		override public esEntity CreateInstance()
		{
			return new TSSystem();
		}
		
		#region Static Quick Access Methods
		static public void Delete(System.Decimal systemId)
		{
			var obj = new TSSystem();
			obj.SystemId = systemId;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save();
		}

	    static public void Delete(System.Decimal systemId, esSqlAccessType sqlAccessType)
		{
			var obj = new TSSystem();
			obj.SystemId = systemId;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save(sqlAccessType);
		}
		#endregion

		
					
		
	
	}



    [DebuggerDisplay("Count = {Count}")]
	[Serializable]
	[CollectionDataContract]
	[XmlType("TSSystemCollection")]
	public partial class TSSystemCollection : esTSSystemCollection, IEnumerable<TSSystem>
	{
		public TSSystem FindByPrimaryKey(System.Decimal systemId)
		{
			return this.SingleOrDefault(e => e.SystemId == systemId);
		}

		
		
		#region WCF Service Class
		
		[DataContract]
		[KnownType(typeof(TSSystem))]
		public class TSSystemCollectionWCFPacket : esCollectionWCFPacket<TSSystemCollection>
		{
			public static implicit operator TSSystemCollection(TSSystemCollectionWCFPacket packet)
			{
				return packet.Collection;
			}

			public static implicit operator TSSystemCollectionWCFPacket(TSSystemCollection collection)
			{
				return new TSSystemCollectionWCFPacket() { Collection = collection };
			}
		}
		
		#endregion
		
				
	}



    [DebuggerDisplay("Query = {Parse()}")]
	[Serializable]	
	public partial class TSSystemQuery : esTSSystemQuery
	{
		public TSSystemQuery():this("TSSystemQuery")
		{
		}
		public TSSystemQuery(string joinAlias)
		{
			this.es.JoinAlias = joinAlias;
		}	

		override protected string GetQueryName()
		{
			return "TSSystemQuery";
		}
		
					
	
		#region Explicit Casts
		
		public static explicit operator string(TSSystemQuery query)
		{
			return TSSystemQuery.SerializeHelper.ToXml(query);
		}

		public static explicit operator TSSystemQuery(string query)
		{
			return (TSSystemQuery)TSSystemQuery.SerializeHelper.FromXml(query, typeof(TSSystemQuery));
		}
		
		#endregion		
	}

	[DataContract]
	[Serializable]
	abstract public partial class esTSSystem : esEntity
	{
		public esTSSystem()
		{

		}
		
		#region LoadByPrimaryKey
		public virtual bool LoadByPrimaryKey(System.Decimal systemId)
		{
			if(this.es.Connection.SqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(systemId);
			else
				return LoadByPrimaryKeyStoredProcedure(systemId);
		}

		public virtual bool LoadByPrimaryKey(esSqlAccessType sqlAccessType, System.Decimal systemId)
		{
			if (sqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(systemId);
			else
				return LoadByPrimaryKeyStoredProcedure(systemId);
		}

		private bool LoadByPrimaryKeyDynamic(System.Decimal systemId)
		{
			TSSystemQuery query = new TSSystemQuery();
			query.Where(query.SystemId == systemId);
			return this.Load(query);
		}

		private bool LoadByPrimaryKeyStoredProcedure(System.Decimal systemId)
		{
			esParameters parms = new esParameters();
			parms.Add("SystemId", systemId);
			return this.Load(esQueryType.StoredProcedure, this.es.spLoadByPrimaryKey, parms);
		}
		#endregion
		
		#region Properties
		
		
		
		/// <summary>
		/// Maps to T_S_System.SystemId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Decimal? SystemId
		{
			get
			{
				return base.GetSystemDecimal(TSSystemMetadata.ColumnNames.SystemId);
			}
			
			set
			{
				if(base.SetSystemDecimal(TSSystemMetadata.ColumnNames.SystemId, value))
				{
					OnPropertyChanged(TSSystemMetadata.PropertyNames.SystemId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_System.SystemName
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String SystemName
		{
			get
			{
				return base.GetSystemString(TSSystemMetadata.ColumnNames.SystemName);
			}
			
			set
			{
				if(base.SetSystemString(TSSystemMetadata.ColumnNames.SystemName, value))
				{
					OnPropertyChanged(TSSystemMetadata.PropertyNames.SystemName);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_System.IsActive
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? IsActive
		{
			get
			{
				return base.GetSystemInt32(TSSystemMetadata.ColumnNames.IsActive);
			}
			
			set
			{
				if(base.SetSystemInt32(TSSystemMetadata.ColumnNames.IsActive, value))
				{
					OnPropertyChanged(TSSystemMetadata.PropertyNames.IsActive);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_System.CreatedBy
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String CreatedBy
		{
			get
			{
				return base.GetSystemString(TSSystemMetadata.ColumnNames.CreatedBy);
			}
			
			set
			{
				if(base.SetSystemString(TSSystemMetadata.ColumnNames.CreatedBy, value))
				{
					OnPropertyChanged(TSSystemMetadata.PropertyNames.CreatedBy);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_System.CreatedTime
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? CreatedTime
		{
			get
			{
				return base.GetSystemDateTime(TSSystemMetadata.ColumnNames.CreatedTime);
			}
			
			set
			{
				if(base.SetSystemDateTime(TSSystemMetadata.ColumnNames.CreatedTime, value))
				{
					OnPropertyChanged(TSSystemMetadata.PropertyNames.CreatedTime);
				}
			}
		}		
		
		#endregion	

		#region .str() Properties
		
		public override void SetProperties(IDictionary values)
		{
			foreach (string propertyName in values.Keys)
			{
				this.SetProperty(propertyName, values[propertyName]);
			}
		}
		
		public override void SetProperty(string name, object value)
		{
			esColumnMetadata col = this.Meta.Columns.FindByPropertyName(name);
			if (col != null)
			{
				if(value == null || value is System.String)
				{				
					// Use the strongly typed property
					switch (name)
					{							
						case "SystemId": this.str().SystemId = (string)value; break;							
						case "SystemName": this.str().SystemName = (string)value; break;							
						case "IsActive": this.str().IsActive = (string)value; break;							
						case "CreatedBy": this.str().CreatedBy = (string)value; break;							
						case "CreatedTime": this.str().CreatedTime = (string)value; break;
					}
				}
				else
				{
					switch (name)
					{	
						case "SystemId":
						
							if (value == null || value is System.Decimal)
								this.SystemId = (System.Decimal?)value;
								OnPropertyChanged(TSSystemMetadata.PropertyNames.SystemId);
							break;
						
						case "IsActive":
						
							if (value == null || value is System.Int32)
								this.IsActive = (System.Int32?)value;
								OnPropertyChanged(TSSystemMetadata.PropertyNames.IsActive);
							break;
						
						case "CreatedTime":
						
							if (value == null || value is System.DateTime)
								this.CreatedTime = (System.DateTime?)value;
								OnPropertyChanged(TSSystemMetadata.PropertyNames.CreatedTime);
							break;
					

						default:
							break;
					}
				}
			}
            else if (this.ContainsColumn(name))
            {
                this.SetColumn(name, value);
            }
			else
			{
				throw new Exception("SetProperty Error: '" + name + "' not found");
			}
		}		

		public esStrings str()
		{
			if (esstrings == null)
			{
				esstrings = new esStrings(this);
			}
			return esstrings;
		}

		sealed public class esStrings
		{
			public esStrings(esTSSystem entity)
			{
				this.entity = entity;
			}
			
	
			public System.String SystemId
			{
				get
				{
					System.Decimal? data = entity.SystemId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.SystemId = null;
					else entity.SystemId = Convert.ToDecimal(value);
				}
			}
				
			public System.String SystemName
			{
				get
				{
					System.String data = entity.SystemName;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.SystemName = null;
					else entity.SystemName = Convert.ToString(value);
				}
			}
				
			public System.String IsActive
			{
				get
				{
					System.Int32? data = entity.IsActive;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.IsActive = null;
					else entity.IsActive = Convert.ToInt32(value);
				}
			}
				
			public System.String CreatedBy
			{
				get
				{
					System.String data = entity.CreatedBy;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CreatedBy = null;
					else entity.CreatedBy = Convert.ToString(value);
				}
			}
				
			public System.String CreatedTime
			{
				get
				{
					System.DateTime? data = entity.CreatedTime;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CreatedTime = null;
					else entity.CreatedTime = Convert.ToDateTime(value);
				}
			}
			

			private esTSSystem entity;
		}
		
		[NonSerialized]
		private esStrings esstrings;		
		
		#endregion
		
		#region Housekeeping methods

		override protected IMetadata Meta
		{
			get
			{
				return TSSystemMetadata.Meta();
			}
		}

		#endregion		
		
		#region Query Logic

		public TSSystemQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new TSSystemQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(TSSystemQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return this.Query.Load();
		}
		
		protected void InitQuery(TSSystemQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntity)this).Connection;
			}			
		}

		#endregion
		
        [IgnoreDataMember]
		private TSSystemQuery query;		
	}



	[Serializable]
	abstract public partial class esTSSystemCollection : esEntityCollection<TSSystem>
	{
		#region Housekeeping methods
		override protected IMetadata Meta
		{
			get
			{
				return TSSystemMetadata.Meta();
			}
		}

		protected override string GetCollectionName()
		{
			return "TSSystemCollection";
		}

		#endregion		
		
		#region Query Logic

	#if (!WindowsCE)
		[BrowsableAttribute(false)]
	#endif
		public TSSystemQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new TSSystemQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(TSSystemQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return Query.Load();
		}

		override protected esDynamicQuery GetDynamicQuery()
		{
			if (this.query == null)
			{
				this.query = new TSSystemQuery();
				this.InitQuery(query);
			}
			return this.query;
		}

		protected void InitQuery(TSSystemQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntityCollection)this).Connection;
			}			
		}

		protected override void HookupQuery(esDynamicQuery query)
		{
			this.InitQuery((TSSystemQuery)query);
		}

		#endregion
		
		private TSSystemQuery query;
	}



	[Serializable]
	abstract public partial class esTSSystemQuery : esDynamicQuery
	{
		override protected IMetadata Meta
		{
			get
			{
				return TSSystemMetadata.Meta();
			}
		}	
		
		#region QueryItemFromName
		
        protected override esQueryItem QueryItemFromName(string name)
        {
            switch (name)
            {
				case "SystemId": return this.SystemId;
				case "SystemName": return this.SystemName;
				case "IsActive": return this.IsActive;
				case "CreatedBy": return this.CreatedBy;
				case "CreatedTime": return this.CreatedTime;

                default: return null;
            }
        }		
		
		#endregion
		
		#region esQueryItems

		public esQueryItem SystemId
		{
			get { return new esQueryItem(this, TSSystemMetadata.ColumnNames.SystemId, esSystemType.Decimal); }
		} 
		
		public esQueryItem SystemName
		{
			get { return new esQueryItem(this, TSSystemMetadata.ColumnNames.SystemName, esSystemType.String); }
		} 
		
		public esQueryItem IsActive
		{
			get { return new esQueryItem(this, TSSystemMetadata.ColumnNames.IsActive, esSystemType.Int32); }
		} 
		
		public esQueryItem CreatedBy
		{
			get { return new esQueryItem(this, TSSystemMetadata.ColumnNames.CreatedBy, esSystemType.String); }
		} 
		
		public esQueryItem CreatedTime
		{
			get { return new esQueryItem(this, TSSystemMetadata.ColumnNames.CreatedTime, esSystemType.DateTime); }
		} 
		
		#endregion
		
	}


	
	public partial class TSSystem : esTSSystem
	{

		#region TSFunctionsCollectionBySystemId - Zero To Many
		
		static public esPrefetchMap Prefetch_TSFunctionsCollectionBySystemId
		{
			get
			{
				esPrefetchMap map = new esPrefetchMap();
				map.PrefetchDelegate = yncTech.BusinessObjects.TSSystem.TSFunctionsCollectionBySystemId_Delegate;
				map.PropertyName = "TSFunctionsCollectionBySystemId";
				map.MyColumnName = "SystemId";
				map.ParentColumnName = "SystemId";
				map.IsMultiPartKey = false;
				return map;
			}
		}		
		
		static private void TSFunctionsCollectionBySystemId_Delegate(esPrefetchParameters data)
		{
			TSSystemQuery parent = new TSSystemQuery(data.NextAlias());

			TSFunctionsQuery me = data.You != null ? data.You as TSFunctionsQuery : new TSFunctionsQuery(data.NextAlias());

			if (data.Root == null)
			{
				data.Root = me;
			}
			
			data.Root.InnerJoin(parent).On(parent.SystemId == me.SystemId);

			data.You = parent;
		}			
		
		/// <summary>
		/// Zero to Many
		/// Foreign Key Name - T_S_Functions_FK1
		/// </summary>

		[XmlIgnore]
		public TSFunctionsCollection TSFunctionsCollectionBySystemId
		{
			get
			{
				if(this._TSFunctionsCollectionBySystemId == null)
				{
					this._TSFunctionsCollectionBySystemId = new TSFunctionsCollection();
					this._TSFunctionsCollectionBySystemId.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("TSFunctionsCollectionBySystemId", this._TSFunctionsCollectionBySystemId);
				
					if (this.SystemId != null)
					{
						if (!this.es.IsLazyLoadDisabled)
						{
							this._TSFunctionsCollectionBySystemId.Query.Where(this._TSFunctionsCollectionBySystemId.Query.SystemId == this.SystemId);
							this._TSFunctionsCollectionBySystemId.Query.Load();
						}

						// Auto-hookup Foreign Keys
						this._TSFunctionsCollectionBySystemId.fks.Add(TSFunctionsMetadata.ColumnNames.SystemId, this.SystemId);
					}
				}

				return this._TSFunctionsCollectionBySystemId;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._TSFunctionsCollectionBySystemId != null) 
				{ 
					this.RemovePostSave("TSFunctionsCollectionBySystemId"); 
					this._TSFunctionsCollectionBySystemId = null;
					
				} 
			} 			
		}
			
		
		private TSFunctionsCollection _TSFunctionsCollectionBySystemId;
		#endregion

		
		protected override esEntityCollectionBase CreateCollectionForPrefetch(string name)
		{
			esEntityCollectionBase coll = null;

			switch (name)
			{
				case "TSFunctionsCollectionBySystemId":
					coll = this.TSFunctionsCollectionBySystemId;
					break;	
			}

			return coll;
		}		
		/// <summary>
		/// Used internally by the entity's hierarchical properties.
		/// </summary>
		protected override List<esPropertyDescriptor> GetHierarchicalProperties()
		{
			List<esPropertyDescriptor> props = new List<esPropertyDescriptor>();
			
			props.Add(new esPropertyDescriptor(this, "TSFunctionsCollectionBySystemId", typeof(TSFunctionsCollection), new TSFunctions()));
		
			return props;
		}
		
		/// <summary>
		/// Called by ApplyPostSaveKeys 
		/// </summary>
		/// <param name="coll">The collection to enumerate over</param>
		/// <param name="key">"The column name</param>
		/// <param name="value">The column value</param>
		private void Apply(esEntityCollectionBase coll, string key, object value)
		{
			foreach (esEntity obj in coll)
			{
				if (obj.es.IsAdded)
				{
					obj.SetProperty(key, value);
				}
			}
		}
		
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PostSave.
		/// </summary>
		protected override void ApplyPostSaveKeys()
		{
			if(this._TSFunctionsCollectionBySystemId != null)
			{
				Apply(this._TSFunctionsCollectionBySystemId, "SystemId", this.SystemId);
			}
		}
		
	}
	



	[Serializable]
	public partial class TSSystemMetadata : esMetadata, IMetadata
	{
		#region Protected Constructor
		protected TSSystemMetadata()
		{
			m_columns = new esColumnMetadataCollection();
			esColumnMetadata c;

			c = new esColumnMetadata(TSSystemMetadata.ColumnNames.SystemId, 0, typeof(System.Decimal), esSystemType.Decimal);
			c.PropertyName = TSSystemMetadata.PropertyNames.SystemId;
			c.IsInPrimaryKey = true;
			c.IsAutoIncrement = true;
			c.NumericPrecision = 18;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSSystemMetadata.ColumnNames.SystemName, 1, typeof(System.String), esSystemType.String);
			c.PropertyName = TSSystemMetadata.PropertyNames.SystemName;
			c.CharacterMaxLength = 50;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSSystemMetadata.ColumnNames.IsActive, 2, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = TSSystemMetadata.PropertyNames.IsActive;
			c.NumericPrecision = 10;
			c.HasDefault = true;
			c.Default = @"((1))";
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSSystemMetadata.ColumnNames.CreatedBy, 3, typeof(System.String), esSystemType.String);
			c.PropertyName = TSSystemMetadata.PropertyNames.CreatedBy;
			c.CharacterMaxLength = 50;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSSystemMetadata.ColumnNames.CreatedTime, 4, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = TSSystemMetadata.PropertyNames.CreatedTime;
			c.IsNullable = true;
			m_columns.Add(c);
				
		}
		#endregion	
	
		static public TSSystemMetadata Meta()
		{
			return meta;
		}	
		
		public Guid DataID
		{
			get { return base.m_dataID; }
		}	
		
		public bool MultiProviderMode
		{
			get { return false; }
		}		

		public esColumnMetadataCollection Columns
		{
			get	{ return base.m_columns; }
		}
		
		#region ColumnNames
		public class ColumnNames
		{ 
			 public const string SystemId = "SystemId";
			 public const string SystemName = "SystemName";
			 public const string IsActive = "IsActive";
			 public const string CreatedBy = "CreatedBy";
			 public const string CreatedTime = "CreatedTime";
		}
		#endregion	
		
		#region PropertyNames
		public class PropertyNames
		{ 
			 public const string SystemId = "SystemId";
			 public const string SystemName = "SystemName";
			 public const string IsActive = "IsActive";
			 public const string CreatedBy = "CreatedBy";
			 public const string CreatedTime = "CreatedTime";
		}
		#endregion	

		public esProviderSpecificMetadata GetProviderMetadata(string mapName)
		{
			MapToMeta mapMethod = mapDelegates[mapName];

			if (mapMethod != null)
				return mapMethod(mapName);
			else
				return null;
		}
		
		#region MAP esDefault
		
		static private int RegisterDelegateesDefault()
		{
			// This is only executed once per the life of the application
			lock (typeof(TSSystemMetadata))
			{
				if(TSSystemMetadata.mapDelegates == null)
				{
					TSSystemMetadata.mapDelegates = new Dictionary<string,MapToMeta>();
				}
				
				if (TSSystemMetadata.meta == null)
				{
					TSSystemMetadata.meta = new TSSystemMetadata();
				}
				
				MapToMeta mapMethod = new MapToMeta(meta.esDefault);
				mapDelegates.Add("esDefault", mapMethod);
				mapMethod("esDefault");
			}
			return 0;
		}			

		private esProviderSpecificMetadata esDefault(string mapName)
		{
			if(!m_providerMetadataMaps.ContainsKey(mapName))
			{
				esProviderSpecificMetadata meta = new esProviderSpecificMetadata();			


				meta.AddTypeMap("SystemId", new esTypeMap("numeric", "System.Decimal"));
				meta.AddTypeMap("SystemName", new esTypeMap("nvarchar", "System.String"));
				meta.AddTypeMap("IsActive", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("CreatedBy", new esTypeMap("nvarchar", "System.String"));
				meta.AddTypeMap("CreatedTime", new esTypeMap("datetime", "System.DateTime"));			
				
				
				
				meta.Source = "T_S_System";
				meta.Destination = "T_S_System";
				
				meta.spInsert = "proc_T_S_SystemInsert";				
				meta.spUpdate = "proc_T_S_SystemUpdate";		
				meta.spDelete = "proc_T_S_SystemDelete";
				meta.spLoadAll = "proc_T_S_SystemLoadAll";
				meta.spLoadByPrimaryKey = "proc_T_S_SystemLoadByPrimaryKey";
				
				this.m_providerMetadataMaps["esDefault"] = meta;
			}
			
			return this.m_providerMetadataMaps["esDefault"];
		}

		#endregion

		static private TSSystemMetadata meta;
		static protected Dictionary<string, MapToMeta> mapDelegates;
		static private int _esDefault = RegisterDelegateesDefault();
	}
}
